;----------------------------------------------------
;
; INSERT.ASM
;
; Sample Custom Command Program.
; Written by CTG
;
; This routine brings up a file requestor showing ANM
; files, and INSERTS all of their characters into the
; current map.  It then adjusts the current map, by
; the number of characters insert.
;
;----------------------------------------------------


ASSUME CS:CODE, ES:NOTHING, SS:NOTHING, DS:CODE

code segment para public 'code'

include Custom.inc		;This is part of the GG developers package.

org	100h
Start:		mov	CS:HostDataSeg,ds
		jmp	SkipOverData

HostDataSeg	dw	0
CharToRotate	dw	0

InstructTxt	db	'This will insert the selected',0dh
		db	'anm file into the char. set.',0
InstructLen	equ	$-InstructTxt



SkipOverData:	test	DS:[EditFlag],-1
		jnz	ItsOk
		retf			;This is not for sprite mode.
ItsOk:
		call	DisplayPromptText
		
		mov	DS:[SearchString],offset CVALUES	;Load .ANM
		mov	word ptr DS:[CVALUES],'.*'		;files.
		mov	word ptr DS:[CVALUES+2],'NA'
		mov	word ptr DS:[CVALUES+4],'M'

		HCALL	GetLoadHandle				;Host call

		cmp	DS:[InputHandle],-1
		jnz	ValidHandle
		jmp	QuickExit				;User exit.
ValidHandle:
		mov	ax,DS:[CharacterCount]

		push	ax				;Current Char count on stack

		push	DS:[MaxXChars]
		push	DS:[MaxYChars]
		push	word ptr DS:[BrushCount]

		mov	dx,offset HeadStuff		;Load ANM file header
		mov	cx,12
		mov	bx,DS:[Handle]
		mov	ah,3fh
		int	21h			;Not dealing with I/O errors.

		Pop	word ptr DS:[BrushCount]	;Restore important
		Pop	DS:[MaxYChars]			;stuff
		Pop	DS:[MaxXChars]

		pop	si
		push	si

		shl	si,4
		mov	cx,si
		add	si,si		;Points to end of characters.

		mov	di,DS:[CharacterCount]
		shl	di,5
		add	di,si
		std

		push	ds
		push	es
		mov	es,DS:[CDATA]
		mov	ds,DS:[CDATA]
		add	cx,2

	    rep movsw			;Characters have been moved over.

		pop	es
	    	pop	ds

		cld

		push	ds
		push	es

		mov	bx,DS:[Handle]		;Load in ANM characters
		mov	cx,DS:[CharacterCount]	;into front of char set.
		xor	dx,dx
		shl	cx,5
		mov	ah,3fh
		mov	ds,DS:[CDATA]
		int	21h			;Not dealing with I/O errors.

		pop	es
		pop	ds


		mov	ah,3eh			;Close file
		mov	bx,DS:[Handle]
		int	21h			;Not dealing with I/O errors.


		push	es

		mov	ax,DS:[CharacterCount]
		mov	es,DS:[MDATA]
		mov	cx,32767
		xor	di,di

NextAdd:	add	ES:[di],ax
		inc	di
		inc	di
		loop	NextAdd

		pop	es

		pop	ax			;Get previous character count

		add	DS:[CharacterCount],ax	;Adjust it.

		
QuickExit:	HCALL	RedrawAll
		retf			;Program terminates.

;-----------------------------------------------------------------
;
; DisplayPromptText
;	Displays the text InstructTXT in a prompt box, and waits
;	for a user keypress or click.
;
;-----------------------------------------------------------------

DisplayPromptText:

		mov	ax,cs			;Copy local static into hosts 
		mov	ds,ax			;data segment.
		mov	di,offset CVALUES
		mov	cx,InstructLen
		mov	si,offset InstructTXT
	    rep movsw
		mov	DS,CS:HostDataSeg	;Restore data segment

		mov	DS:[PromptText],offset CVALUES
		mov	DS:[MaxInputLength],0
		HCALL	GetBoxedInput
		HCALL	DrawEntireScreen
		ret


code ends
end Start
